#include "stdafx.h"
#include "GameObject.h"

namespace WindGE
{
	GameObject::GameObject()
	{
	}

	GameObject::GameObject(const std::string& name)
	{
	}

	GameObject::~GameObject()
	{
	}

	const XMFLOAT3& GameObject::euler_angle() const
	{
		static XMFLOAT3 rot(0.0f, 0.0f, 0.0f);

		float sqx = rotation_.x * rotation_.x;
		float sqy = rotation_.y * rotation_.y;
		float sqz = rotation_.z * rotation_.z;
		float sqw = rotation_.w * rotation_.w;
		float unit = sqx + sqy + sqz + sqw;
		float test = rotation_.w * rotation_.x - rotation_.y * rotation_.z;
		if (test > 0.499999f * unit)
		{
			// singularity at north pole
			rot.y = 2 * atan2(rotation_.z, rotation_.w);	//yaw
			rot.z = Pi / 2;		//roll
			rot.x = 0;			//pitch
		}
		else
		{
			if (test < -0.499999f * unit)
			{
				// singularity at south pole
				rot.y = -2 * atan2(rotation_.z, rotation_.w);	//yaw
				rot.z = -Pi / 2;	//roll
				rot.x = 0;			//pitch
			}
			else
			{
				/*yaw*/   rot.y = atan2(2 * (rotation_.y * rotation_.w + rotation_.x * rotation_.z), -sqx - sqy + sqz + sqw);
				/*pitch*/ rot.x = asin(2 * test / unit);
				/*roll*/  rot.z = atan2(2 * (rotation_.z * rotation_.w + rotation_.x * rotation_.y), -sqx + sqy - sqz + sqw);
			}
		}

		rot.x *= 180.0f / Pi;
		rot.y *= 180.0f / Pi;
		rot.z *= 180.0f / Pi;

		return rot;
	}

	GameObjectPtr GameObject::get_child(unsigned int i) const
	{
		GameObjectPtr child = nullptr;
		if (i < children_.size())
		{
			if (children_[i]) child = GameObjectPtr(children_[i]);
		}
		return child;
	}

}//end namespace WindGE

